<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  website
 */

class m_website_actions_block_MovePageAction extends m_backoffice_extends_FormModal
{
	public function execute()
	{
		$page = $this->getDocumentProvider()->getByUniqueId($this->getRequestParameter("docId"), $this->getRequestParameter("docLang"));
		$formValues = $this->getFormValues("form");
		$relManager = f_relation_Manager::getInstance();
		$parent = $relManager->getUniqueParent($page->getUniqueId(), $page->getLang());		

        $folders = m_backoffice_services_TreeService::getInstance()->getSelectArray($this->getContextWebsite()->getUniqueId(), array("modules_website/website", "modules_website/menu"), $this->getContextWebsite()->getUniqueId(), true, true, 'website');
		
		if(count($formValues) > 0)
		{
			$destMenu = $this->getDocumentProvider()->getByUniqueId($formValues["folder"], $page->getLang());
			
			$relManager->clearRelation($parent, $page, "page", $page->getLang());
			$relManager->setRelation($destMenu, $page, "page", null, $page->getLang());
			
			$errors["status"] = "success";
			$this->addToModel("formValues", $formValues);
			$this->addToModel("errors", $errors);
		}
		
		$this->addToModel("href", $this->getRequestParameter("href"));
		$this->addToModel("folders", $folders);
		$this->addToModel("currentFolderId", $page->getDocumentParent()->getUniqueId());
		$this->addToModel("page", $page);
		$this->setView('success');
	}	
	
	public function validate()
	{
		return $this->getUserContext()->hasRight(array("backoffice_accessBo", "website_Edit"));
	}	
}